package frame;

import javax.swing.*;
import java.awt.*;

import static frame.GuanliPanel.jedis;

public class gerenxinxi extends JInternalFrame {
    private JTextArea textArea;

    public gerenxinxi() {
        super("个人信息", true, true, true, true);
        this.setLayout(new BorderLayout());
        this.setSize(500, 400); // 增大窗口尺寸

        // 中央面板
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 设置边距
        centerPanel.add(Box.createVerticalGlue()); // 添加垂直间距

        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font("SansSerif", Font.PLAIN, 16)); // 设置更大的字体
        textArea.setLineWrap(true); // 自动换行
        textArea.setWrapStyleWord(true); // 断行不断字
        textArea.setMargin(new Insets(10, 10, 10, 10)); // 设置内边距

        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(400, 250)); // 增大首选尺寸
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); // 设置边框

        centerPanel.add(scrollPane);
        centerPanel.add(Box.createVerticalGlue()); // 添加垂直间距

        this.add(centerPanel, BorderLayout.CENTER);

        // 加载员工信息
        loadEmployeeInfo();
    }

    private void loadEmployeeInfo() {
        String eid = LandPanel.getyonghu1();
        // 先从 Redis 中获取员工信息
        String employeeInfo = jedis.hget("employees", eid);
        if (employeeInfo != null) {
            // Redis 中存在缓存，直接显示
            displayEmployeeInfo(employeeInfo);
        } else {
            textArea.setText("未找到员工信息");
        }
    }

    private void displayEmployeeInfo(String employeeInfo) {
        try {
            System.out.println("Received employee info: " + employeeInfo); // 调试语句
            // 解析员工信息
            String[] infoParts = employeeInfo.split(",");
            System.out.println("Info parts length: " + infoParts.length); // 调试语句
            if (infoParts.length >= 7) {
                // 显示员工信息
                StringBuilder sb = new StringBuilder();
                sb.append("ID: ").append(infoParts[0].trim()).append("\n")
                        .append("姓名: ").append(infoParts[1].trim()).append("\n")
                        .append("密码: ").append(infoParts[2].trim()).append("\n")
                        .append("职位: ").append(infoParts[3].trim()).append("\n")
                        .append("电话: ").append(infoParts[4].trim()).append("\n")
                        .append("工资: ").append(infoParts[5].trim()).append("\n")
                        .append("其他信息: ").append(infoParts[6].trim());

                textArea.setText(sb.toString());
            } else {
                textArea.setText("员工信息格式错误");
            }
        } catch (Exception e) {
            textArea.setText("显示员工信息出错：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
